using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

using Yorrick.FlickrMetadataSynchr.Data;

namespace Yorrick.FlickrMetadataSynchr.Match
{
   /// <summary>
   /// Class that can match pictures based on their metadata in multiple passes using
   /// different strategies.
   /// </summary>
   internal class MultiPassMetadataMatcher : MetadataMatcherBase
   {
      #region Private fields
      /// <summary>
      /// The metadata matchers to use.
      /// </summary>
      IList<IMetadataMatcher> _metadataMatchers;
      #endregion

      #region Constructor
      /// <summary>
      /// Constructs an instance.
      /// </summary>
      /// <param name="metadataMatchers">The metadata matchers to use.</param>
      public MultiPassMetadataMatcher(IList<IMetadataMatcher> metadataMatchers)
      {
         _metadataMatchers = metadataMatchers;
      }
      #endregion

      #region Private methods
      /// <summary>
      /// Performs the matching of Flickr pictures with local pictures.
      /// </summary>
      /// <remarks>This method should only be called once.</remarks>
      protected override void DoMatch()
      {
         IList<LocalPictureMetadata> unmatchedLocalPictures = LocalPicturesToMatch;
         IList<FlickrPictureMetadata> unmatchedFlickrPictures = FlickrPicturesToMatch;

         foreach (IMetadataMatcher metadataMatcher in _metadataMatchers)
         {
            metadataMatcher.SetPictureLists(unmatchedLocalPictures, unmatchedFlickrPictures);
            
            MatchedPictures.AddRange(metadataMatcher.GetMatchedPictures());

            unmatchedLocalPictures = metadataMatcher.GetUnmatchedLocalPictures();
            unmatchedFlickrPictures = metadataMatcher.GetUnmatchedFlickrPictures();
         }

         UnmatchedLocalPictures.AddRange(unmatchedLocalPictures);
         UnmatchedFlickrPictures.AddRange(unmatchedFlickrPictures);
      }
      #endregion
   }
}
